//
//  LGMonitoringSettingView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/13.
//

#import "LGMonitoringSettingView.h"
#import "LGPickerView.h"
@interface LGMonitoringSettingView()

/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *selectBtn;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UILabel *frequencyTitle;
/**   */
@property (nonatomic , strong)UIButton *frequencyBtn;
/**   */
@property (nonatomic , strong)UILabel *frequencyLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;

@end

@implementation LGMonitoringSettingView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setDataDic:(NSMutableDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.iconView.image = [UIImage imageNamed:[NSString stringWithFormat:@"体征监测-%@",self.titleLabel.text]];
    self.selectBtn.selected = ([dataDic[@"is_check"] integerValue]>0)?YES:NO;
    [self.frequencyBtn setTitle:[NSString stringWithFormat:@"每日%@次",dataDic[@"num"]] forState:UIControlStateNormal];
    [self.frequencyBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
}

-(void)selectBtnTouched:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSString *isCheck = (sender.selected == YES)?@"1":@"0";
    [self.dataDic setValue:isCheck forKey:@"is_check"];
    if(sender.selected == YES && [self.dataDic[@"num"] integerValue] == 0){
        [self frequencyBtnAction];
    }
}

-(void)frequencyBtnAction{
    LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.titleLabel.text = @"监测频率";
    pickerView.pickArry = @[@[@"1",@"2",@"3",@"4",@"5"]];
    pickerView.selectStr = LGNSString(self.dataDic[@"num"]);
    UILabel *titleLabel = [UILabel lableWithText:@"每日" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentRight lines:1];
    [pickerView.pickView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pickerView.pickView);
        make.right.equalTo(pickerView.mas_centerX).offset(-viewPix(60));
    }];
    UILabel *unitLabel = [UILabel lableWithText:@"次" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    [pickerView.pickView addSubview:unitLabel];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pickerView.pickView);
        make.left.equalTo(pickerView.mas_centerX).offset(viewPix(65));
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        NSString *result = LGNSString(resultArray[0]);
        [weakSelf.dataDic setValue:result forKey:@"num"];
        [weakSelf.frequencyBtn setTitle:[NSString stringWithFormat:@"每日%@次",result] forState:UIControlStateNormal];
    };
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.iconView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.selectBtn];
    [self addSubview:self.lineView];
    [self addSubview:self.frequencyTitle];
    [self addSubview:self.frequencyBtn];
    [self addSubview:self.tipView];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-viewPix(10));
        make.centerY.equalTo(self.titleLabel);
        make.height.equalTo(@(viewPix(33)));
        make.width.equalTo(@(viewPix(100)));
    }];
//    [self.frequencyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.tipView.mas_left).offset(-viewPix(5));
//        make.centerY.equalTo(self.tipView);
//    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-viewPix(12));
        make.centerY.equalTo(self.frequencyTitle);
        make.width.height.equalTo(@(viewPix(12)));
    }];
}

-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(14), viewPix(16), viewPix(16))];
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(34), viewPix(12), viewPix(150), viewPix(20));
    }
    return _titleLabel;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithImage:@"unselect21" selectImage:@"selected24"];
        _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_selectBtn addTarget:self action:@selector(selectBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(45), Screen_W-viewPix(56), 0.5)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
    }
    return _lineView;
}

-(UILabel *)frequencyTitle{
    if(!_frequencyTitle){
        _frequencyTitle = [UILabel lableWithText:@"监测频率" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _frequencyTitle.frame = CGRectMake(viewPix(12), viewPix(57), viewPix(100), viewPix(23));
    }
    return _frequencyTitle;
}

-(UIButton *)frequencyBtn{
    if(!_frequencyBtn){
        _frequencyBtn = [UIButton buttonWithTitle:@"每日1次" titleFont:LGFont(16) textColor:@"#666666" imageName:@"grayTip12"];
        
        _frequencyBtn.frame = CGRectMake(0, viewPix(52), Screen_W-viewPix(32)-viewPix(12), viewPix(33));
        _frequencyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_frequencyBtn addTarget:self action:@selector(frequencyBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_frequencyBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
    }
    return _frequencyBtn;
}

@end
